package com.zmn.plat.admin.controller.part;

import java.util.Objects;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dictionary.GlobalPartUnitDict;
import com.zmn.plat.model.entity.part.PartCategory;
import com.zmn.plat.model.entity.part.PartSkuQuery;
import com.zmn.plat.model.vo.part.PartSkuVO;

/**
 * 类描述： PartSkuListController
 *
 * @author DaiZhichao
 * @since 2018/11/6 20:36
 */
@Controller
@RequestMapping("/part")
public class PartSkuListController extends PartBaseController {

    private Logger logger = LoggerFactory.getLogger(PartSkuListController.class);

    /**
     * 描述: 跳转到配件列表页
     *
     * @author DaiZhichao
     * @since 2018/11/6 20:34
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute PartSkuQuery query, Model model) {

        model.addAttribute("brandSimpleList", super.getBrandSimpleList());
        model.addAttribute("categOnes", partCategoryService.getPartCategorySimpleList(GlobalConsts.TOP_ID));
        model.addAttribute("statuses", GlobalDict.getStatusList(false));

        return "part/list";
    }

    /**
     * 描述: 跳转到配件详情页（添加修改）
     *
     * @author DaiZhichao
     * @since 2018/11/6 20:34
     */
    @RequestMapping(value = {"/addEdit"})
    @RequiresPermissions("/part/addEdit.action")
    public String addedit(@RequestParam(name = "skuId", required = false) Integer skuId, Model model) {

        PartSkuVO partSkuVO = new PartSkuVO() {
            {
                setSkuId(0);
                setStatus((short)2);
            }
        };

        if (Objects.nonNull(skuId)) {
            partSkuVO = partSkuService.findVOByKey(skuId);
            if (partSkuVO.getCategOneId() != null) {
                PartCategory oneCateg = partCategoryService.findByKey(partSkuVO.getCategOneId());
                if (oneCateg != null) {
                    partSkuVO.setProductType(oneCateg.getProductType());
                }
            }
        }
        model.addAttribute("part", partSkuVO);
        model.addAttribute("unitList", GlobalPartUnitDict.getUnitList());
        model.addAttribute("brandSimpleList", super.getBrandSimpleList());
        model.addAttribute("categOnes", partCategoryService.getPartCategorySimpleList(GlobalConsts.TOP_ID));
        model.addAttribute("categTwos", partCategoryService.getPartCategorySimpleList(partSkuVO.getCategOneId()));
        model.addAttribute("statuses", GlobalDict.getStatusList(false));

        return "part/addedit";
    }

}
